//
//  CXHToBidBannerAdapter.m
//  CXHAdSDK-ToBid
//
//  Created by 陈坤 on 2025/4/4.
//

#import "CXHToBidBannerAdapter.h"
#import <CXHAdSDK-Core/CXHAdSDKBannerAdView.h>
#import <WindFoundation/WindFoundation.h>

@interface CXHToBidBannerAdapter ()<CXHSDKBannerAdViewDelegate>

@property (nonatomic, weak) id<AWMCustomBannerAdapterBridge> bridge;
@property (nonatomic, strong) CXHAdSDKBannerAdView *bannerAdView;
@property (nonatomic, assign) BOOL isReady;

@end

@implementation CXHToBidBannerAdapter

- (instancetype)initWithBridge:(id<AWMCustomBannerAdapterBridge>)bridge {
    self = [super init];
    if (self) {
        _bridge = bridge;
    }
    return self;
}
- (void)loadAdWithPlacementId:(NSString *)placementId parameter:(AWMParameter *)parameter {
    NSString *sizeStr = [self.bridge.adRequest.options objectForKey:@"adSize"];
    CGSize adSize = CGSizeFromString(sizeStr);
    if (CGSizeEqualToSize(CGSizeZero, adSize)) {
        NSError *error = [NSError errorWithDomain:@"strategy" code:-60004 userInfo:@{NSLocalizedDescriptionKey:@"adSize设置错误"}];
        [self.bridge bannerAd:self didLoadFailWithError:error ext:nil];
        return;
    }
    UIViewController *viewController = [self.bridge viewControllerForPresentingModalView];
    self.bannerAdView = [[CXHAdSDKBannerAdView alloc] initWithFrame:CGRectMake(0, 0, adSize.width, adSize.height)];
    self.bannerAdView.delegate = self;
    self.bannerAdView.posId = parameter.placementId;
    self.bannerAdView.controller = viewController;
    [self.bannerAdView loadAndShow];
}
- (BOOL)mediatedAdStatus {
    return self.isReady;
}
- (void)didReceiveBidResult:(AWMMediaBidResult *)result {
    if (result.win) {
        [self.bannerAdView sendWinNotice:result.winnerPrice];
        return;
    }
    // todo: 当胜出时，winnerPrice有值，其它情况不回传胜出价
    [self.bannerAdView sendLossNotice:result.winnerPrice];
}
- (void)destory {
    self.isReady = NO;
    [self.bannerAdView removeFromSuperview];
    self.bannerAdView = nil;
}

#pragma mark - CXHSDKBannerAdViewDelegate
/**
 广告获取成功
 
 @param bannerView banner实例
 */
- (void)CXH_bannerViewDidReceived:(CXHAdSDKBannerAdView *)bannerView{
    WindmillLogDebug(@"[cxh]", @"%s", __func__);
    NSString *price = [NSString stringWithFormat:@"%ld", [self.bannerAdView getBidPrice]];
    [self.bridge bannerAd:self didAdServerResponse:bannerView ext:@{
        AWMMediaAdLoadingExtECPM: price
    }];
    self.isReady = YES;
    [self.bridge bannerAd:self didLoad:bannerView];
}

/**
 广告拉取失败
 
 @param bannerView banner实例
 @param error 错误描述
 */
- (void)CXH_bannerViewFailToReceived:(CXHAdSDKBannerAdView *)bannerView error:(NSError *)error{
    WindmillLogDebug(@"[cxh]", @"%s", __func__);
    self.isReady = NO;
    [self.bridge bannerAd:self didLoadFailWithError:error ext:nil];
}

/**
 广告点击
 
 @param bannerView 广告实例
 @param loadingPageURL 广告落地页地址，当渠道为bwt，并且customLoadingPage为YES时有值
 */
- (void)CXH_bannerViewClicked:(CXHAdSDKBannerAdView *)bannerView loadingPageURL:(NSString *)loadingPageURL{
    WindmillLogDebug(@"[cxh]", @"%s", __func__);
    [self.bridge bannerAdDidClick:self bannerView:bannerView];
}

/**
 广告点击上报
 
 @param bannerView 广告实例
 */
- (void)CXH_bannerViewClickedReport:(CXHAdSDKBannerAdView *)bannerView{
    
}

/**
 广告关闭
 
 @param bannerView 广告实例
 */
- (void)CXH_bannerViewClose:(CXHAdSDKBannerAdView *)bannerView{
    WindmillLogDebug(@"[cxh]", @"%s", __func__);
    [self.bridge bannerAdDidClosed:self bannerView:bannerView];
}

/**
 广告展示
 
 @param bannerView 广告实例
 */
- (void)CXH_bannerViewExposure:(CXHAdSDKBannerAdView *)bannerView{
    WindmillLogDebug(@"[cxh]", @"%s", __func__);
    [self.bridge bannerAdDidBecomeVisible:self bannerView:bannerView];
}

/**
 广告展示上报
 
 @param bannerView 广告实例
 */
- (void)CXH_bannerViewExposureReport:(CXHAdSDKBannerAdView *)bannerView{
    
}

/**
 关闭落地页
 
 @param bannerView 广告实例
 */
- (void)CXH_bannerAdCloseLandingPage:(CXHAdSDKBannerAdView *)bannerView{
    
}

@end
